{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3.15 \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u63a5\u53d7\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u8f93\u5165\uff0c\u4f46\u662f\u4f60\u60f3\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a datetime \u5bf9\u8c61\u4ee5\u4fbf\u5728\u4e0a\u9762\u6267\u884c\u975e\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528Python\u7684\u6807\u51c6\u6a21\u5757 datetime \u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from datetime import datetime\ntext = '2012-09-20'\ny = datetime.strptime(text, '%Y-%m-%d')\nz = datetime.now()\ndiff = z - y\ndiff"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "datetime.strptime() \u65b9\u6cd5\u652f\u6301\u5f88\u591a\u7684\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\n\u6bd4\u5982 %Y \u4ee3\u88684\u4f4d\u6570\u5e74\u4efd\uff0c %m \u4ee3\u8868\u4e24\u4f4d\u6570\u6708\u4efd\u3002\n\u8fd8\u6709\u4e00\u70b9\u503c\u5f97\u6ce8\u610f\u7684\u662f\u8fd9\u4e9b\u683c\u5f0f\u5316\u5360\u4f4d\u7b26\u4e5f\u53ef\u4ee5\u53cd\u8fc7\u6765\u4f7f\u7528\uff0c\u5c06\u65e5\u671f\u8f93\u51fa\u4e3a\u6307\u5b9a\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6bd4\u5982\uff0c\u5047\u8bbe\u4f60\u7684\u4ee3\u7801\u4e2d\u751f\u6210\u4e86\u4e00\u4e2a datetime \u5bf9\u8c61\uff0c\n\u4f60\u60f3\u5c06\u5b83\u683c\u5f0f\u5316\u4e3a\u6f02\u4eae\u6613\u8bfb\u5f62\u5f0f\u540e\u653e\u5728\u81ea\u52a8\u751f\u6210\u7684\u4fe1\u4ef6\u6216\u8005\u62a5\u544a\u7684\u9876\u90e8\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "z"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "nice_z = datetime.strftime(z, '%A %B %d, %Y')\nnice_z"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c strptime() \u7684\u6027\u80fd\u8981\u6bd4\u4f60\u60f3\u8c61\u4e2d\u7684\u5dee\u5f88\u591a\uff0c\n\u56e0\u4e3a\u5b83\u662f\u4f7f\u7528\u7eafPython\u5b9e\u73b0\uff0c\u5e76\u4e14\u5fc5\u987b\u5904\u7406\u6240\u6709\u7684\u7cfb\u7edf\u672c\u5730\u8bbe\u7f6e\u3002\n\u5982\u679c\u4f60\u8981\u5728\u4ee3\u7801\u4e2d\u9700\u8981\u89e3\u6790\u5927\u91cf\u7684\u65e5\u671f\u5e76\u4e14\u5df2\u7ecf\u77e5\u9053\u4e86\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u786e\u5207\u683c\u5f0f\uff0c\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u4e00\u5957\u89e3\u6790\u65b9\u6848\u6765\u83b7\u53d6\u66f4\u597d\u7684\u6027\u80fd\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u77e5\u9053\u6240\u4ee5\u65e5\u671f\u683c\u5f0f\u662f YYYY-MM-DD \uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9e\u73b0\u4e00\u4e2a\u89e3\u6790\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from datetime import datetime\ndef parse_ymd(s):\n    year_s, mon_s, day_s = s.split('-')\n    return datetime(int(year_s), int(mon_s), int(day_s))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u9645\u6d4b\u8bd5\u4e2d\uff0c\u8fd9\u4e2a\u51fd\u6570\u6bd4 datetime.strptime() \u5feb7\u500d\u591a\u3002\n\u5982\u679c\u4f60\u8981\u5904\u7406\u5927\u91cf\u7684\u6d89\u53ca\u5230\u65e5\u671f\u7684\u6570\u636e\u7684\u8bdd\uff0c\u90a3\u4e48\u6700\u597d\u8003\u8651\u4e0b\u8fd9\u4e2a\u65b9\u6848\uff01"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}